package com.eqdd.yiqidian.ui.mine.shenqing.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.CoolRefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewFragmentCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LizhiPizhunInfoResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.mine.pizhun.MyPiZhunInfoActivity;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by lvzhihao on 17-7-20.
 */

public class LizhiShengqingFragment extends BaseFragment {
    CoolRefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<LizhiPizhunInfoResultBean> slimAdapterEx;
    private int status;

    @Override
    protected void setView() {
    }

    private void refresh() {
        OkGo.<HttpResult<List<LizhiPizhunInfoResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.APPALY_LIZHI_LIST)
                .params("userGuid", GreenDaoUtil.getUser().getGuid())
                .params("status", status)
                .execute(new DialogCallBack<HttpResult<List<LizhiPizhunInfoResultBean>>>((BaseActivity) getActivity()) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<LizhiPizhunInfoResultBean>>> response) {
                        HttpResult<List<LizhiPizhunInfoResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }

                    @Override
                    public void onError(Response<HttpResult<List<LizhiPizhunInfoResultBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }

    @Override
    protected void initData() {
        if (slimAdapterEx == null) {
            status = getArguments().getInt(Config.STATUS);
            RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
                refresh();
            });
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_12, new SlimInjector<LizhiPizhunInfoResultBean>() {
                @Override
                public void onInject(LizhiPizhunInfoResultBean data, IViewInjector injector) {

                    injector.text(R.id.tv_upper_left_content, data.getCompany())
                            .text(R.id.tv_upper_right_content, data.getDepartment() + " " + data.getPost())
                            .text(R.id.tv_down_left_content, data.getQuitTime())
                            .text(R.id.tv_down_right_content, data.getQuitReason());
                }
            })
                    .attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener((recyclerView, position, v) -> {
                        Bundle bundle = new Bundle();
                        bundle.putInt(Config.STATUS, status);
                        bundle.putInt(Config.WHO, 0);
                        bundle.putString("quitId", slimAdapterEx.getData().get(position).getQuitId() + "");
                        startActivity(MyPiZhunInfoActivity.class, bundle);
                    });
            dataBinding.coolRefreshView.setRefreshing(true);
        }
    }


    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_cool_recycler_view, null, false);

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.APPLY_LIZHI) {
            dataBinding.coolRefreshView.setRefreshing(true);
        }
    }
}
